# PyQuine
#
# Copyright (c) 2007 Alexis Couronne <www.skitoo.org>
#
# This program is free software; you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation; either version 2 of the License, or
# (at your option) any later version.
# 
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with this program; if not, write to the Free Software
# Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
# 

import random
from sqlobject import *


class CardboardError(Exception):
    pass

class Cardboard( SQLObject ):
    firstLine  = PickleCol( )
    secondLine = PickleCol( )
    thirdLine  = PickleCol( )   
    player     = ForeignKey( 'Player', cascade='null', notNone=False, default=None )
    
    def __init__(self, **kw):
        self._numbers = list() 
        SQLObject.__init__(self, **kw)               
                       
    def _set_firstLine(self, value):
        self._verifLine(value) 
        self._SO_set_firstLine(value) 
    
    def _set_secondLine(self, value):
        self._verifLine(value)  
        self._SO_set_secondLine(value) 
        
    def _set_thirdLine(self, value):
        self._verifLine(value) 
        self._SO_set_thirdLine(value) 
    
    def _verifLine(self, line):
        if not isinstance( line, tuple ):
            raise TypeError( "Line must be a Tuple" )
        if len( line ) != 5:
            raise TypeError( "Line must be a Tuple of 5 Integer" )        
        template = { "0|9":0, "10|19":0, "20|29":0, "30|39":0, "40|49":0, "50|59":0, "60|69":0, "70|79":0, "80|90":0 }
        keys = template.keys()
        keys.sort()
        for val in line:
            if not isinstance( val, int ):
                raise TypeError( "Line must be a Tuple of 5 Integer" )
            if val in self._numbers:
                raise CardboardError( "A number can be only once in Cardboad" )
            elif val < 1 or val > 90:
                raise CardboardError( "A number can be only between 1 and 90" )            
            else:
                self._numbers.append(val)
            for key in keys:
                min, max = key.split( '|' )
                min, max = int(min), int(max)
                if val >= min and val <= max:
                    if template[key] == 1:
                         raise CardboardError( "2 numbers or more are in the same ten" ) 
                    else:
                        template[key] += 1      
        
def makeRandomCardboad():    
    while True:
        numbers = range(1, 91)
        line1, line2, line3 = [],[],[]
        while True:
            number  = random.choice( numbers )
            numbers.remove( number )
            if len( line1 ) < 5:
                line1.append( number )
            elif len( line2 ) < 5:
                line2.append( number )
            elif len( line3 ) < 5:
                line3.append( number )
            else:
                try:
                    cardboard = Cardboard( firstLine=tuple( line1 ), secondLine=tuple( line2 ), thirdLine=tuple( line3 ) )
                except:
                    break
                else:
                    return cardboard
    
if __name__ == '__main__':
    for i in range(1000):
        makeRandomCardboad()